<template>
  <div id="login">
    <el-form class="login-form" :model="form" label-width="80px">
      <el-form-item label="用户名：">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="密码：">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <div style="text-align: center;">
        <el-button type="primary" style="width: 30%;max-width: 150px;" @click="onSubmit">登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
  import {ref} from 'vue'
  import { getUsers, userLogin } from '@/api/user.js'
  import {useRouter} from 'vue-router'
  import md5 from 'js-md5'
  import Add from '../pages/test-add-router.vue'
  import {useStore} from 'vuex'

  let form = ref({})
  let router = useRouter()
  let store = useStore()

  function onSubmit() {
    form.value.password = md5(form.value.password)
    userLogin(form.value).then(res => {
      const {name, password} = res
      if (name==='wenhui' && password==='81dc9bdb52d04dc20036dbd8313ed055'){
        localStorage.setItem('token', res.password)
        router.push({path: '/home'})

        let tempRouter = {
          path: 'add',
          name: 'Add',
          component: Add,
          meta: {
            hidden: false
          }
        }
        router.addRoute('path-app', tempRouter)
        router.addRoute({
          path: '/:pathMatch(.*)',
          redirect: '/404',
          meta: {
            hidden: true
          }
        })
        console.log(router.getRoutes())
        store.dispatch('addRouter', {
          tempRouter,
          router
        })
      }
    })
  }
</script>

<style scoped lang="scss">
  #login {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background: #666666;
    .login-form {
      width: 500px;
      position: absolute;
      top: 50%;
      left: 50%;
      margin: 0 auto;
      transform: translate(-50%, -55%);
      padding: 50px 20px;
      background: rgba(255,255,255,0.2);
      border-radius: 6px;
      box-shadow: 1px 1px 2px rgba(0,0,0,0.1);
    }
  }
</style>
